package com.example.SocialApplication.activity.lists.fragments;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import com.example.SocialApplication.HelperFactory;
import com.example.SocialApplication.R;
import com.example.SocialApplication.activity.lists.CreateListStatic;
import com.example.SocialApplication.adapters.ThingsAdapter;
import com.example.SocialApplication.db.entity.Thing;
import com.example.SocialApplication.db.entity.ThingMain;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AddThingFragment extends Fragment {
    private List<ThingMain> mListThings;
    private ThingsAdapter mAdapter;
    private LinearLayout mScrollLayout;
    private List<Thing> mListThingsToSave;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            mListThings = HelperFactory.getHelper().getThingMainDAO().queryForAll();
            mListThingsToSave = new ArrayList<Thing>();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_list_add_thing, container, false);

        mScrollLayout = (LinearLayout) rootView.findViewById(R.id.fragment_list_add_thing_horizontal_scroll_view);

        GridView gridView = (GridView) rootView.findViewById(R.id.fragment_list_add_thing_grid_view);
        mAdapter = new ThingsAdapter(getActivity(), R.layout.color_spectrum_item, mListThings);
        gridView.setAdapter(mAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                showAddDialog(mAdapter.getItem(position));
            }
        });
        return rootView;
    }

    private void showAddDialog(final ThingMain item) {
        final Dialog dialog = new Dialog(getActivity());
        Button buttonAdd = new Button(dialog.getContext());
        buttonAdd.setText("Add thing");
        dialog.addContentView(buttonAdd, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        buttonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addThingToScroll(item);
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    private void addThingToScroll(ThingMain thing) {
        Thing mThing = new Thing();
        mThing.setColor(thing.getColor());
        mThing.setPictureId(thing.getPictureId());
        mThing.setName(thing.getName());

        View view = getActivity().getLayoutInflater().inflate(R.layout.color_spectrum_item, null, false);

        LinearLayout layout = (LinearLayout) view.findViewById(R.id.background_layout);
        ImageView image = (ImageView) view.findViewById(R.id.imageView);
        TextView name = (TextView) view.findViewById(R.id.name);

        layout.setBackgroundColor(Color.parseColor(thing.getColor()));
        image.setImageBitmap(getBitmap(thing.getPictureId()));
        name.setText(thing.getName());

//        mListThingsToSave.add(mThing);
        CreateListStatic.insertThing(mThing);
        mScrollLayout.addView(view);
        LinearLayout layout1 = new LinearLayout(mScrollLayout.getContext());
        mScrollLayout.addView(layout1, new LinearLayout.LayoutParams(5, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    private Bitmap getBitmap(String name) {
        String pathToFolder = getActivity().getExternalCacheDir().getAbsolutePath()
                + File.separator
                + "images"
                + File.separator;

        String fileName = File.separator + name + ".png";
        String path = pathToFolder + fileName;

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap result = BitmapFactory.decodeFile(path, options);
        return result;
    }
}
